package com.itheima.service;

import com.alibaba.druid.sql.visitor.functions.Right;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RightDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version V1.0
 * @author: hanshitong
 * @date: 2019-08-08 10:01
 * @description:
 */
@Service(interfaceClass = RightService.class)
@Transactional
public class RightServiceImpl implements RightService {

    @Autowired
    private RightDao rightDao;
    @Autowired
    private RoleDao roledao;

    /**
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return  分页查询
     */
    @Override
    public PageResult rightAll(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> pages = rightDao.findPage(queryString);
        return new PageResult(pages.getTotal(), pages.getResult());
    }

    /**
     *  添加权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        rightDao.add(permission);
    }

//    编辑查询
    @Override
    public Permission findById(Integer id) {
       return rightDao.findById(id);
    }

//    编辑修改
    @Override
    public void edit(Permission permission) {
        rightDao.edit(permission);
    }

    /**
     *  权限删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
       Role data =  roledao.findRoleByID(id);
       if (data==null)
       {
           rightDao.delete(id);
       }
    }

    @Override
    public List<Permission> findAll() {
        return rightDao.findAll();
    }
}